package com.my.mvvmrapid.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

/**
 * Created by zhongjh on 2021/4/13.
 *
 */
public class HomeShopLinearLayout extends LinearLayout {

    private float startY;
    private boolean isRefreshing;

    public HomeShopLinearLayout(Context context) {
        super(context);
    }

    public HomeShopLinearLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public HomeShopLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public HomeShopLinearLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        //消费掉（即不走自身的onTouch,也不走父容器的onTouch）
        if (isRefreshing) {
            startY = 0;
            return true;
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        float x = ev.getRawX();
        float y = ev.getRawY();
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            startY = y;
        }
        return super.onInterceptTouchEvent(ev);
    }

    /**
     * @return 获取初始的Y位置
     */
    public float getStartY() {
        return startY;
    }

    /**
     * 设置是否刷新中，如果刷新中就消费掉事件
     * @param refreshing 刷新
     */
    public void setRefreshing(boolean refreshing) {
        isRefreshing = refreshing;
    }

}
